package v1

import (
	"encoding/json"
	"fmt"
	"gateway-api/controllers"
	"gateway-api/tools"

	"github.com/gin-gonic/gin"
)

/*
 +----------------------------------------------------------------------
 + Title        : IndexController
 + Author       : huwl
 + Version      : 1.0.0
 + Initial-Time : 2025-07-23
 + Last-time    : 这个文件最后修改的时间 + 修改人的名称
 + Desc         : 首页控制层
 +----------------------------------------------------------------------
*/
type IndexController struct {
	controllers.BaseController
}

func (control IndexController) Index(ctx *gin.Context) {
	// tools.ZLogger.Error(fmt.Sprintf("index error :%s", 59))
	// tools.ZLogger.Error(fmt.Sprintf("safwgvsdvasfasdfddaf"))
	control.BaseSuccess(ctx, control.WithMessage("欢迎使用三叉戟框架前端接口 v1.0.0"))
}

// MINIO上传单张图片
func (control IndexController) MinioSingleUploadImage(ctx *gin.Context) {
	minioImageUpload, err := tools.NewMinioImageUpload()
	if err != nil {
		control.BaseError(ctx, control.WithMessage(err.Error()))
		return
	}

	fmt.Println("minioImageUpload =", minioImageUpload)

	minioImageUpload1, err := tools.NewMinioImageUpload()
	if err != nil {
		control.BaseError(ctx, control.WithMessage(err.Error()))
		return
	}

	fmt.Println("minioImageUpload1 =", minioImageUpload1)

	control.BaseSuccess(ctx, control.WithMessage("MINIO上传单张图片"))
}

// 本地上传单张图片
func (control IndexController) SingleUploadImage(ctx *gin.Context) {
	image, err := ctx.FormFile("file")
	if err != nil {
		control.BaseError(ctx, control.WithMessage("请选择上传图片"))
		return
	}
	thumbs := []map[string]string{}
	water := map[string]string{}

	// thumbs = append(thumbs, map[string]string{
	// 	"width":  "200",
	// 	"height": "200",
	// })

	sui := new(tools.SingleUploadImage)
	result, err := sui.SingleUploadImage(ctx, image, thumbs, water)
	if err != nil {
		control.BaseError(ctx, control.WithMessage(err.Error()))
		return
	}

	control.BaseSuccess(ctx, control.WithData(gin.H{
		"image_url":          result["image_url"],
		"absolute_image_url": result["absolute_image_url"],
	}), control.WithMessage("上传图片成功"))
}

// 测试请求参数
func (control IndexController) TestRequestParams(ctx *gin.Context) {
	params := ctx.PostFormArray("params")
	fmt.Println("params ===", params)
	values := []map[string]interface{}{}
	for _, param := range params {
		value := map[string]interface{}{}
		json.Unmarshal([]byte(param), &value)
		values = append(values, value)
		fmt.Println("value =", value)
	}

	control.BaseSuccess(ctx, control.WithData(values))
}
